#!/bin/bash
#作者：XiTong
#rsync+inotify一键实时同步
#代码内部删除的文件都是由此脚本创建的文件
clear
echo -e "\033[31m ____  ____  
|  _ \/ ___| 
| |_) \___ \ 
|  _ < ___) |
|_| \_\____/ 
\033[0m"
echo -e "\033[33m欢迎使用实时同步工具\033[0m\n-----------------------\n\033[34m本工具还在不断完善\033[0m\n\033[36m说明：只需输入一些客户端信息以及相关目录方可同步(输入信息时一定要确保输入正确)\n自动检测依赖的软件包并安装\n自动开启相关依赖服务\n请提前配置防火墙与安全策略防止同步失败\033[0m\n\033[33m当无客户端IP可输入时请输入n\033[0m\n------------------------"
rm -rf /opt/.ips
rm -rf /opt/.rs.sh
rm -rf /opt/.d
while true
do
file=$(echo $1)
if [ "$file" = "-h" ]; then
	ip=$(echo $2)
	ping -c 1 $ip &> /dev/null
	if [ $? = 0 ]; then
		break
	else
		echo "输入的IP有误或者IP连接不到，请重新输入！！！"
		exit
	fi
else
echo "------------------------"
shu=1
while :
do
read -p "请输入客户端的IP($shu)：" ip
if [ "$ip" == "n" ]; then
	echo "即将对`echo $[$shu-1]`台主机进行同步..."
	break
else
	echo "$ip" >> /opt/.ips
	((shu++))
fi
done
fi
hangshu=$(cat /opt/.ips | wc -l)
for ((i=1;i<=$hangshu; i++))
do
ping -c 1 `cat /opt/.ips | awk "NR==$i{print}"` &> /dev/null
if [ $? = 0 ]; then
	echo "客户端("$i")ok"
	echo "yes" > /opt/.test
else
	echo "输入的IP有误或者IP连接不到，请重新输入！！！"
	> /opt/.ips
	echo "no" > /opt/.test
	continue
fi
done
tes=$(cat /opt/.test)
if [ "$tes" == "yes" ]; then
	rm -rf /opt/.test
	break
else
	echo "------------------------"
fi
done
masterip=$(echo $SSH_CONNECTION | cut -d" " -f3)
#检测私钥公钥
while true
do
check=$(ls /root/.ssh/ | grep id_rsa | wc -l)
if [ $check = 2 ]; then
	echo -e "\033[33m已有私钥公钥文件，即将进行下一步...\033[0m"
	break
else
	echo "检测到未有私钥公钥文件，即将生成公钥与私钥..."
	cd ~
	ssh-keygen
fi
done
while true
do
echo "即将进入配置免密登录界面..."
for ((i=3; i>=0; i--))
do
echo -ne "$i\b"
sleep 1
done
echo "------------------------"
for ((i=1; i<=$hangshu; i++))
do
echo "正在配置第"$i"台主机的免密登录"
ssh-copy-id -i /root/.ssh/id_rsa.pub root@`cat /opt/.ips | awk "NR==$i{print}"`
checkpub=$(echo $?)
if [ $checkpub = 0 ]; then
	echo -e "\033[33m第"$i"台免密登录设置成功，即将进行下一步...\033[0m"
	echo "yes" > /opt/test
else
	echo -e "\033[31m免密设置失败，请检查密码是否正确...\033[0m"
fi
done
tes=$(cat /opt/test)
if [ $tes == "yes" ]; then
	rm -rf /opt/test
	break
fi
done
#检测rsync
while true
do
	rpm -q rsync &> /dev/null
	checkrs=$(echo $?)
	if [ $checkrs = 0 ]; then
		echo -e "\033[33mrsync已安装，即将进行下一步...\033[0m"
		break
	else	
		echo "检测到rsync未安装，即将安装rsync..."
		yum install rsync -y &> /dev/null
	fi
done
#检测inotify
while true
do
	checkep=$(ls /etc/yum.repos.d/ | grep epel | wc -l)
	if [ $checkep -gt 0 ]; then
		echo -e "\033[33mepel源已安装，即将进行下一步...\033[0m" &> /dev/null
		rpm -q inotify-tools &> /dev/null
		checkin=$(echo $?)
		if [ $checkin = 0 ]; then
			echo -e "\033[33minotify已安装，即将进行下一步...\033[0m"
			break
		else
			echo "检测到未安装inotify，即将安装inotify..."
			yum install inotify-tools -y &> /dev/null
		fi
	else
		rpm -q inotify-tools &> /dev/null
		checkin=$(echo $?)
		if [ $checkin = 0 ]; then
			echo -e "\033[33minotify已安装，即将进行下一步...\033[0m"
			break
		else
			echo "检测到未安装inotify，即将安装inotify..."
			yum -y install epel-release &> /dev/null
			yum install inotify-tools -y &> /dev/null
		fi	
fi
done
#检测客户端是否安装
while true
do
for ((i=1;i<=$hangshu; i++))
do
ssh root@`cat /opt/.ips | awk "NR==$i{print}"` 'rpm -q rsync'
if [ $? = 0 ]; then
	echo -e "\033[33m客户端("$i")已经安装rsync...\033[0m"
	echo "yes" > /opt/test
else
	echo "检测到客户端("$i")未安装rsync，即将进行安装..."
	ssh root@`cat /opt/.ips | awk "NR==$i{print}"` 'yum install -y rsync'
fi
done
if [ $tes == "yes" ]; then
        rm -rf /opt/test
        break
fi
done	
while :
do
	echo "------------------------"
	read -p "请输入要同步的本地目录：" dd
			ls $dd &> /dev/null
		if [ $? = "0" ]; then
			break
		else
			echo "输入的目录不存在，请重新输入..."
		fi
done
echo "------------------------"
for ((i=1;i<=$hangshu; i++))
do
	while :
	do
		read -p "请输入要同步的客户端("$i")目录：" dds
		ssh root@`cat /opt/.ips | awk "NR==$i{print}"` "ls $dds" &> /dev/null
		if [ $? = 0 ]; then
			echo "$dds" >> /opt/.d
			break
		else
			echo "你输入的目录在客户端("$i")不存在，请重新输入..."
		fi
	done
echo "------------------------"
#创建文件导入客户端
echo -e "# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area\nuid = nobody\ngid = nobody\nuse chroot = no\nmax connections = 10\npid file = /var/run/rsyncd.pid\nlock file = /var/run/rsync.lock\nlog file = /var/log/rsyncd.log\n\n[web"$i"]\npath = `cat /opt/.d | awk "NR==$i{print}"`\ncomment = web$i\nread only = no\nwrite only = no\nhosts allow = $masterip\nhosts deny = *\nuid = root\ngid = root\nauth users = web"$i"user\nsecrets file = /opt/web"$i".pass" > /opt/.rsync.conf
scp /opt/.rsync.conf `cat /opt/.ips | awk "NR==$i{print}"`:/etc/rsyncd.conf
checkcp=$(echo $?)
if [ $checkcp = 0 ]; then
	echo -e "\033[33m客户端("$i")文件导入成功...\033[0m"
	#创建密码文件
	echo "123456" > /opt/rsyncd.secrets
	chmod 600 /opt/rsyncd.secrets
	ssh root@`cat /opt/.ips | awk "NR==$i{print}"` "echo "web"$i"user:123456" > /opt/web"$i".pass"
	ssh root@`cat /opt/.ips | awk "NR==$i{print}"` "chmod 600 /opt/web"$i".pass"
	echo "正在启动客户端("$i")rsyncd服务..."
	ssh root@`cat /opt/.ips | awk "NR==$i{print}"` 'systemctl start rsyncd'
	if [ $i = $hangshu ]; then
	echo "正在启动服务端rsyncd服务..."
	systemctl restart rsyncd
	checkre=$(echo $?)
	if [ $checkre = 0 ]; then
		echo -e "\033[33m服务启动成功，进入同步信息界面：\033[0m\n---------------------------------"
	else
		echo -e "\033[31m服务启动失败...\033[0m"
	fi
fi
else
	echo -e "\033[31m客户端($i)文件导入失败...\033[0m"
fi
done
	
#配置服务端
for ((i=1;i<=$hangshu; i++))
do
echo "host"$i"=`cat /opt/.ips | awk "NR==$i{print}"`" >> /opt/.rs.sh
echo "dst"$i"=web$i" >> /opt/.rs.sh
echo "user"$i"=web"$i"user" >> /opt/.rs.sh
echo "src"$i"=$dd" >> /opt/.rs.sh
done

echo "/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib \$src1 \\
| while read files
do" >> /opt/.rs.sh
for ((i=1;i<=$hangshu; i++))
do
echo '/usr/bin/rsync -vzrtopg --delete --progress --password-file=/opt/rsyncd.secrets $src'$i' $user'$i'@$host'$i'::$dst'$i'' >> /opt/.rs.sh
done
echo "done" >> /opt/.rs.sh
/bin/bash /opt/.rs.sh
